package cn.edu.ctbu.datajpa1.service;

import cn.edu.ctbu.datajpa1.domain.Teacher;
import cn.edu.ctbu.datajpa1.repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class TeacherService {
    @Autowired
    private TeacherRepository teacherRepository;
    public List<Teacher> findAll(){

        return teacherRepository.findAll();
    }
    /**
     按id进行查询
     *@param id 主键，整数
     *@return
     */
    public Teacher findById(Integer id){
        return teacherRepository.findById(id).orElse(null);
    }


    /**
     * 按姓名查找
     * @param name
     * @return
     */
    public List<Teacher> findByName(String name){

        return teacherRepository.findByNameLike(name);
    }



    public void insert(Teacher teacher){

        teacherRepository.save(teacher);
    }

    public void update(Teacher teacher){

        teacherRepository.save(teacher);
    }

    public void delete(Integer id){

        teacherRepository.deleteById(id);
    }


    public List<Teacher> findLastTeacher() {

        return teacherRepository.findTeacherWithSamePrefix();
    }

}
